<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/27
 * Time: 19:38
 */


require_once "config.php";
require_once "MyPDO.class.php";
$db = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');

$user = $_COOKIE['user'];

if (!$_POST['yhk'] || !$_POST['money'] || !$_POST['name'] || !$_POST['img']) {
    $json['status'] = "error";
    $json['msg']    = '请填写完整';
    echo json_encode($json, 1);
    exit();
}


$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

if ($redis->exists("sjMoney-" . $user)) {
    $ttl            = $redis->ttl("sjMoney-" . $user);
    $json['status'] = "error";
    $json['msg']    = "操作频繁，请于" . (($ttl - ($ttl % 60)) / 60) . "分钟" . ($ttl % 60) . "秒后重试";
    echo json_encode($json, 1);
    exit();
}

$redis->setex("sjMoney-" . $user, 60 * 5, 1);


$userInfo = $db->query("`cooperation`", "`user` = '$user'", "Row");


$update['yhk']      = $_POST['yhk'];
$update['money']    = $_POST['money'];
$update['name']     = $_POST['name'];
$update['img']      = $_POST['img'];
$update['user']     = $_COOKIE['user'];
$update['date']     = date("Y-m-d H:i:s");
$update['balance']  = $userInfo['money'];
$update['bank']     = "6217002980109072483-中国建设银行-刘芳瑛";
$update['bankName'] = "刘芳瑛";

if ($db->insert("sjMoney", $update)) {
    $json['status'] = 'success';
    $json['msg']    = '提交成功，等待审核';
} else {
    $json['status'] = 'error';
    $json['msg']    = '提交失败，请重新尝试';
}
echo json_encode($json, 1);